//
//  LeftMenuViewController.m
//  WonderfulNotes
//
//  Created by 千锋 on 16/1/6.
//  Copyright © 2016年 andy. All rights reserved.
//

#import "LeftMenuViewController.h"
#import "UserInfoViewController.h"
#import "LoginViewController.h"
#import "headImageView.h"
#import "AboutOurViewController.h"

@interface LeftMenuViewController ()
//头像
@property (weak, nonatomic) IBOutlet headImageView *userIcon;
//用户名
@property (weak, nonatomic) IBOutlet UILabel *userLabel;


@end

@implementation LeftMenuViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
     self.view.backgroundColor = [UIColor clearColor];
    
    [self userRefresh:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userRefresh:) name:USER_REFRESH_NOTICE object:nil];
    
    // Do any additional setup after loading the view from its nib.
}

-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



/** 刷新用户信息*/
-(void)userRefresh:(NSNotification *)notice{
    
//    NSString * userName = [[InfoStore sharedStore]nameForKey:@"UserName"];
//    if (!userName) {
//        self.userLabel.text = @"点击头像登陆";
//    }else{
//        self.userLabel.text = userName;
//    }
    
//   获取用户的信息
    BmobUser * user =[BmobUser getCurrentUser];
    
    if (user) {
        self.userLabel.text = user.username;
        NSLog(@"---------%@",user.username);
        
        [self.userIcon sd_setImageWithURL:[NSURL URLWithString:[user objectForKey:@"userIconUrl"]]];
    
    }else{
    
        self.userLabel.text =@"点击头像登陆";
        self.userIcon.image = [UIImage imageNamed:@"avatar_default_big"];
            
        }
    
//    UIImage * userIcon = [[InfoStore sharedStore]imageForKey:@"UserIcon"];
//    NSString * userName = [[InfoStore sharedStore]nameForKey:@"UserName"];
//    if (!userName) {
//        self.userLabel.text = @"点击头像登陆";
//    }else{
//        self.userLabel.text = userName;
//    }
//    if (!userIcon) {
//        self.userIcon.image = [UIImage imageNamed:@"avatar_default_big"];
//    }else{
//        self.userIcon.image = userIcon;
//    }
    


}

/** 通知传值后一定要把它移除*/
-(void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self forKeyPath:USER_REFRESH_NOTICE];
}


//头像点击手势
- (IBAction)userIconTapAction:(UITapGestureRecognizer *)sender {
    
    UIViewController * vc = nil;
    
    //获取当前用户的信息
    if ([BmobUser getCurrentUser]) {
        
        UserInfoViewController * userInfoVC = [self.storyboard instantiateViewControllerWithIdentifier:@"UserInfoViewController"];
        vc = userInfoVC;
        
    }else{
        //登陆
        LoginViewController * loginVC = [self.storyboard instantiateViewControllerWithIdentifier:@"LoginViewController"];
        vc = loginVC;
    }
    
    //获取中心视图
    UINavigationController * nav = (UINavigationController *)self.sideMenuViewController.contentViewController;
    
    //跳转的时候隐藏当前视图
    [self.sideMenuViewController hideMenuViewController];
    [nav pushViewController:vc animated:YES];
    
}

//所有笔记
- (IBAction)AllNoteBtnClicked:(UIButton *)sender {
    
    UIStoryboard * MainSB = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    [self.sideMenuViewController presentViewController:[MainSB instantiateViewControllerWithIdentifier:@"ResideViewController"] animated:YES completion:nil];
    
}

- (IBAction)SynchronizeCloundBtnClicked:(UIButton *)sender {
    
    [SVProgressHUD  showWithStatus:@"正在同步..."];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(arc4random()%3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
       
       [SVProgressHUD dismiss];
       
   });
    
}

- (IBAction)ClearRubbishBtnClicked:(UIButton *)sender {
    
    //缓存文件的个数
    NSUInteger diskCount = [SDImageCache sharedImageCache].getDiskCount;
    //获取缓存的大小
    NSUInteger cacheSize = [[SDImageCache sharedImageCache] getSize];
    
    NSString * msg = [NSString stringWithFormat:@"缓存文件数量:%lu,缓存文件大小:%.2fM",(unsigned long)diskCount,(cacheSize/1024.0)/1024.0];
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"清除缓存" message:msg preferredStyle:UIAlertControllerStyleAlert];
    //添加Action
    UIAlertAction * cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        [alertController dismissViewControllerAnimated:YES completion:nil];
    }];
    [alertController addAction:cancel];
    
    //清除Action
    UIAlertAction * clearAction = [UIAlertAction actionWithTitle:@"清除缓存" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
        [[SDImageCache sharedImageCache] clearDisk];
        [[SDImageCache sharedImageCache] clearMemory];
    }];
    [alertController addAction:clearAction];
    
    //显示UIAlertController
    [self presentViewController:alertController animated:YES completion:nil];

}
- (IBAction)AboutOurBtnClecked:(UIButton *)sender {
   
    UIStoryboard * MainSb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    AboutOurViewController * AbVC = [MainSb instantiateViewControllerWithIdentifier:@"AboutOurViewController"];
    UINavigationController * Nav = [[UINavigationController alloc]initWithRootViewController:AbVC];
    
    [self.sideMenuViewController hidesBottomBarWhenPushed];
    
    
    [self presentViewController:Nav animated:YES completion:nil];
    
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
